package pk.nak.client.activity;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Map;

import pk.nak.client.R;
import pk.nak.data.EatOut;
import pk.nak.data.EatOutAttributeValue;
import pk.nak.enums.AppVars;
import pk.nak.enums.ExtraIDs;
import pk.nak.enums.TossDownURLs;
import pk.nak.requests.CheckedInRequest;
import pk.nak.requests.EatOutAttributesRequest;
import pk.nak.requests.GetEatOutMenuRequest;
import pk.nak.responses.CheckInResponse;
import pk.nak.responses.EatOutAttributessResponse;
import pk.nak.responses.GetEatOutMenuResponse;
import pk.nak.utils.FormattingUtils;
import android.app.Activity;
import android.app.ProgressDialog;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class EatOutDetailsActivity extends Activity {

	/** Called when the activity is first created. */
	private CheckInResponse responseCheckIn = new CheckInResponse(){

		@Override
		public void onSuccess(String response) {
			// TODO Auto-generated method stub
			super.onSuccess(response);
			AppVars.pdWait.dismiss();
			Toast.makeText(getApplicationContext(),"You are checked in successfully", Toast.LENGTH_LONG).show();
		}
		
	};
	private GetEatOutMenuResponse response = new GetEatOutMenuResponse(){

		@Override
		public void onSuccess(String response) {
			// TODO Auto-generated method stub
			super.onSuccess(response);
			AppVars.pdWait.dismiss();
			EatOut eo = AppVars.selectedEatOut;
			eo.setMenu(this.Menu());

			if ( this.Menu() != null ){
				Intent menuIntent = new Intent(getApplicationContext(), EatOutMenuActivity.class);
				menuIntent.putExtra(ExtraIDs.SELECTED_EATOUT_SERIALIZABLE,eo);
				startActivity(menuIntent);
			}else{
	    		Toast.makeText(getApplicationContext(), "No Menu is defined yet", Toast.LENGTH_LONG).show();
			}
		}
	};
	
	private EatOutAttributessResponse responseAttr = new EatOutAttributessResponse(){

		@Override
		public void onSuccess(String response) {
			// TODO Auto-generated method stub
			super.onSuccess(response);
			AppVars.pdWait.dismiss();
			DrawControls(this.getHtAttributes());
		}
		
	};
	public EatOut selectedEatOut;
	private ArrayList<Spinner> alSP;
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    this.selectedEatOut = (EatOut)this.getIntent().getSerializableExtra(ExtraIDs.SELECTED_EATOUT_SERIALIZABLE);
	    this.setContentView(R.layout.eatoutdetails);
	    this.PopulateData();
	    this.attachedHandlers();
	    // TODO Auto-generated method stub
	}
	
	private void DrawControls(Hashtable<String, EatOutAttributeValue[]> parm){
		try{
			LinearLayout ll = (LinearLayout) findViewById(R.id.llContainer);
			if ( ll != null ){
				this.alSP = new ArrayList<Spinner>();
				for (Map.Entry<String, EatOutAttributeValue[]> obj : parm.entrySet()){
					TextView tv = new TextView(this);
					ll.addView(tv);
					tv.setText(obj.getKey());
					
					Spinner sp = new Spinner(this);
					ll.addView(sp);
					
					ArrayList<EatOutAttributeValue> al = new ArrayList<EatOutAttributeValue>();
					for (EatOutAttributeValue eav:obj.getValue()){
						al.add(eav);
					}
					ArrayAdapter<EatOutAttributeValue> aaItems = new ArrayAdapter<EatOutAttributeValue>(this, android.R.layout.simple_spinner_item,al);
					sp.setAdapter(aaItems);
					this.alSP.add(sp);
				}
			}
			
		}catch(Exception ex){
			Toast.makeText(this, ex.getMessage(), Toast.LENGTH_LONG).show();
		}
	}
	
	private void PopulateData(){
		try{
			EatOutAttributesRequest request = new EatOutAttributesRequest();
			request.doRequst(responseAttr);
			
			TextView tvName = (TextView)findViewById(R.id.tvEatOutName2);
			if ( tvName != null ) tvName.setText(this.selectedEatOut.getEatoutName());
			
			TextView tvDesc = (TextView)findViewById(R.id.tvDescription);
			if ( tvDesc != null ) tvDesc.setText(this.selectedEatOut.getEatoutDescription());
			
			TextView tvHalal = (TextView)findViewById(R.id.tvHalal);
			if ( tvHalal != null ) tvHalal.setText(this.selectedEatOut.getEatoutHalal());
			
			
			ImageView ivLogo = (ImageView)findViewById(R.id.ivLogo);
			if (ivLogo != null ) ivLogo.setImageBitmap(FormattingUtils.loadBitmap(TossDownURLs.LOGO_PREFIX + this.selectedEatOut.getEatoutLogo()));
			
		}catch(Exception ex){
			Toast.makeText(this, ex.getMessage(), Toast.LENGTH_LONG).show();
		}
	}

	private void attachedHandlers(){
		final Button btnMenu = (Button)findViewById(R.id.btnMenu);
		final Button btnMap = (Button)findViewById(R.id.btnMap);
		
		btnMenu.setOnClickListener(new View.OnClickListener() {	
			public void onClick(View v) {
				// TODO Auto-generated method stub
				EatOut eo = ((EatOutDetailsActivity)v.getContext()).selectedEatOut;
				AppVars.selectedEatOut = eo;
				GetEatOutMenuRequest request = new GetEatOutMenuRequest(eo.getEatoutID());
				request.doRequest(response);
				AppVars.pdWait = ProgressDialog.show(v.getContext(), "Loading Menu", "Loading");
			}
		});
		
		btnMap.setOnClickListener(new View.OnClickListener() {	
			public void onClick(View v) {
				EatOut eo = ((EatOutDetailsActivity)v.getContext()).selectedEatOut;
				AppVars.selectedEatOut = eo;
				startActivity(new Intent(v.getContext(), EatOutMapActivity.class));
			}
		});
		
		final Button btnCheckIn = (Button)findViewById(R.id.btnCheckIn);
		btnCheckIn.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String attributes = "";
				for ( Spinner sp : alSP){
					EatOutAttributeValue eoav = (EatOutAttributeValue) sp.getSelectedItem();
					
					if ( attributes != "" ){
						attributes += ";";
					}
					
					attributes += eoav.getId();
				}
				EatOut eo = ((EatOutDetailsActivity)v.getContext()).selectedEatOut;
				CheckedInRequest request = new CheckedInRequest(AppVars.LogedInUserID, eo.getEatoutID(), attributes);
				request.doRequest(responseCheckIn);
				AppVars.pdWait = ProgressDialog.show(v.getContext(), "Doing Checking", "Please Wait...");
			}
		});
	}
}
